package trygeocoding.example.android;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.google.android.maps.GeoPoint;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private EditText editLong;
	private EditText editLat;
	private Button btnWhere;
	public Location myLocation;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editLong =(EditText)findViewById(R.id.editLong);
        editLat=(EditText)findViewById(R.id.editLat);
        btnWhere=(Button)findViewById(R.id.button1);
        btnWhere.setOnClickListener(this);
        myLocation= new Location("");
        double t= 10.798994;
        double t1=106.679636;
        myLocation.setLatitude(t);
        myLocation.setLongitude(t1);
        String name=Geocoder.ReverseGeocode(myLocation);
        Toast.makeText(this, name, 0).show();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id=v.getId();
		switch(id)
		{
		case R.id.button1:
			String name=Geocoder.ReverseGeocode(myLocation);
			Toast.makeText(this, name, 0);
		}
	}
	public static class Geocoder
	{
		public static String ReverseGeocode(Location loc)
		{
			String localName="";
			HttpURLConnection connection =null;
			URL url =null;
			try{
				url=new URL("http://maps.google.com/maps/geo?q=" + Double.toString(loc.getLatitude()) + "," + Double.toString(loc.getLongitude()) +
		        		"&output=xml&oe=utf8&sensor=true&key=06HlmoNS4OvkKz9TnEmxM4w6_viooxqk9nDe2Zg");
				connection=(HttpURLConnection)url.openConnection();
				connection.setRequestMethod("GET");
				connection.setDoOutput(true);
				connection.setReadTimeout(10000);
				connection.connect();
				try{
					InputStreamReader isr=new InputStreamReader(connection.getInputStream());
					InputSource is=new InputSource(isr);
					SAXParserFactory factory= SAXParserFactory.newInstance();
					SAXParser parser=factory.newSAXParser();
					XMLReader xr=parser.getXMLReader();
					Handle handler = new Handle();
					xr.setContentHandler(handler);
		            xr.parse(is);
		            
		            localName = handler.getLocalityName();
		            if(localName =="")
		            {
		            	localName="null";
		            }
		       
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			return localName;
		}
		
		
	}
}
